/**
 * 视频监控实时数据videoDevData.js
 * 版本号： v1.0.0
 */
$(function () {
    //加载所属项目
    bindCombo_project("prId_");

    /**
     *视频设备实时数据列表
     */
    var lastChecked = null;
    $("#videoDevData_dataList").datagrid({
        fit: true,
        fitColumns: true,
        method: "POST",
        rownumbers: true,
        singleSelect: true,
        pagination: true,
        striped: true,
        border: false,
        pageSize: 10,
        pageList: [10, 30, 50, 100],
        url: CONTEXT_PATH + '/videoDevData/videoDevDataList.do',
        idField: 'prId',
        frozenColumns: [[
            {field: 'prId', checkbox: true}
        ]],
        columns: [
            [
                {field: 'userUuid', title: '用户id', width: 180, align: 'left', hidden: true},
                {field: 'videoUrl', title: '视频路径', width: 180, align: 'left', hidden: true},
                {field: 'prName', title: '工程项目', width: 180, align: 'left', fixed: true},
                {field: 'hkUserName', title: '所属视频用户', width: 100, fixed: true},
                {field: 'pbUnitName', title: '建设单位', width: 150, align: 'left', fixed: true},
                {field: 'poUnitName', title: '施工单位', width: 150, align: 'left', fixed: true},
                {field: 'psUnitName', title: '监理单位', width: 150, align: 'left'}
            ]
        ],
        onCheck: function (rowIndex, rowData) {
            var row = $("#videoDevData_dataList").datagrid("getChecked");
            if (row && rowIndex == lastChecked) {
                $('#videoDevData_dataList').datagrid("unselectAll");//取消选中当前所有行
                lastChecked = null;
            } else {
                lastChecked = rowIndex;
            }
        },
        onLoadSuccess: function (data) {
            if (data.rows.length > 0) {
                lastChecked = null;
                $("#videoDevData_dataList").datagrid("selectRow", 0);
            }
        }
    });

    //定义可操作按钮
    $("#videoDevData_menu").dialog({
        title: '',
        width: 'auto',
        height: 35,
        closed: false,
        cache: false,
        inline: true,
        modal: false,
        border: false,
        toolbar: createToolbar()
    });

});

/**
 * 创建toolbar按钮组
 * @returns {Array}
 */
function createToolbar() {
    var toolbar = [];
    var falg = false;
    for (var j = 0; j < btnOptions.length; j++) {
        if (false != btnOptions[j].value) {
            falg = true;
            break;
        }
    }
    if (!falg) {
        //不存在按钮
        toolbar.push({});
    } else {
        //存在按钮
        for (var i = 0; i < btnOptions.length; i++) {
            if (false != btnOptions[i].value) {
                //查询按钮
                if ("searchBtn" == btnOptions[i].key) {
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler: function () {
                            videoDevData_doSearch();
                        }
                    });
                }
                //查看设备列表
                if ("deviceListBtn" == btnOptions[i].key) {
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler: function () {
                            deviceList();
                        }
                    });
                }
                //查看视频
                if ("viewBtn" == btnOptions[i].key) {
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler: function () {
                            nowData();
                        }
                    });
                }
                //详情按钮
                if ("refreshBtn" == btnOptions[i].key) {
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler: function () {
                            videoDevData_view();
                        }
                    });
                }
            }
        }
    }
    return toolbar;
}


/**
 * 查看所有设备数据
 * @param index
 */
function nowData() {
    var checked = $("#videoDevData_dataList").datagrid("getChecked");
    if (!checked || checked.length == 0) {
        $.messager.show({title: '操作提示', msg: "请选择一条数据后再进行修改操作", timeout: 3000, showType: "slide"});
        return;
    }
    ;
    var row = $('#videoDevData_dataList').datagrid('getSelected');
    var userUuid = row.userUuid;
    var videoUrl = row.videoUrl;
    var url = CONTEXT_PATH + "/videoDevData/toVideoUrl.do?userUuid=" + userUuid + "&videoUrl=" + videoUrl;
    window.parent.layout("查看视频", url, "", true);
}

/**
 * 查看项目设备列表
 * @param index
 */
function deviceList() {
    var checked = $("#videoDevData_dataList").datagrid("getChecked");
    if (!checked || checked.length == 0) {
        $.messager.show({title: '操作提示', msg: "请选择一条数据后再进行修改操作", timeout: 3000, showType: "slide"});
        return;
    }
    ;
    var row = $("#videoDevData_dataList").datagrid("getSelected");
    var prId = row.prId;
    var onLineStatus = $("#onlineStatus_").combobox("getValue");
    window.location.href = CONTEXT_PATH + "/videoDevData/toVideoDeviceList.do?prId=" + prId + "&onLineStatus=" + onLineStatus;

}

/**
 * 执行查询
 */
function videoDevData_doSearch() {
    //刷新dg页码
    refreshPageNumber("videoDevData_dataList");
    $("#videoDevData_dataList").datagrid("reload", {
        prId: $("#prId_").combobox("getValue"),
        onLineStatus: $("#onlineStatus_").combobox("getValue")
    });
    $("#videoDevData_dataList").datagrid("clearSelections");
    $('#videoDevDataQuery_dialog').dialog('close');
}

/**
 * 查看详情
 */
function videoDevData_view() {
    var checked = $("#videoDevData_dataList").datagrid("getChecked");
    if (!checked || checked.length == 0) {
        $.messager.show({title: '操作提示', msg: "请选择需要查看的视频详情", timeout: 3000, showType: "slide"});
        return;
    }
    var row = $("#videoDevData_dataList").datagrid("getSelected");

    //IFRAM打开窗口
    layoutSub(row.prId , CONTEXT_PATH + "/videoDevData/videoDevDataDesc.do?id=" + row.prId, 'menu_search', true, "视频详情");
}

/**
 * 所属项目bind事件
 * @param projectControl
 */
function bindCombo_project(projectControl) {
    $.ajax({
        url: CONTEXT_PATH + "/project/getProjectListForCombo.do",
        type: "get",
        success: function (data) {
            var obj = JSON.parse(data);
            var projectCombo = [{'text': '--请选择--', 'value': ''}];
            for (var i = 0; i < obj.length; i++) {
                projectCombo.push({"text": obj[i].prName, "value": obj[i].prId});
            }
            $("#" + projectControl).combobox("loadData", projectCombo);
        }
    })
}


